//
//  UINavigationBar+barStyle.m
//  webViewLocal
//
//  Created by wjc on 2021/11/27.
//

#import "UINavigationBar+barStyle.h"
#import "UIImage+Extension.h"
#import "WJCDefine.h"
@implementation UINavigationBar (barStyle)
///1.关于下划线的处理：
-(void)setNoLine

{
    if(@available(iOS 13.0, *))
    {
        self.standardAppearance.shadowColor = [UIColor clearColor];
    } else{
        if(@available(iOS 10.0, *)){
            [self getLineHidden:YES];
            
        }else{
            self.shadowImage = [UIImage new];
            
        }
    }
}

///2.关于背景色的处理：
- (void)setBackColor:(UIColor *)backColor

{
    self.barTintColor = backColor;
    self.tintColor = backColor;
    //    [bar setTitleTextAttributes:@{NSFontAttributeName:Bfont20,
    //                                  NSForegroundColorAttributeName : HEXC(0x232323)
    //
    //    }];
    [self setShadowImage:[[[UIImage alloc] init] imageWithColor:[UIColor clearColor]]];
    [self setBackgroundImage:[[[UIImage alloc] init] imageWithColor:[UIColor clearColor]] forBarMetrics:UIBarMetricsDefault];
    if (@available(iOS 13.0, *)) {
        UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
        [appearance configureWithOpaqueBackground];
        appearance.backgroundColor = backColor;
        self.standardAppearance = appearance;
        self.scrollEdgeAppearance=appearance;
    }

    
}

///3.关于导航条透明的处理：
- (void)setBackClear
{
    if (@available(iOS 13.0, *))
    {
        [self.standardAppearance configureWithTransparentBackground];
        self.standardAppearance.shadowColor = [UIColor clearColor];
    }else{
        [self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
        if (@available(iOS 10.0, *)){
            [self getLineHidden:YES];
        }else{
            self.shadowImage = [UIImage new];
        }
        
    }
    
}

///4.关于重置导航条为系统的样式处理：
- (void)resetBack

{
    if (@available(iOS 13.0, *))
            {
                    [self.standardAppearance configureWithDefaultBackground];
                    self.standardAppearance.shadowImage = nil;
                }else
                    {
                    [self setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
                    if (@available(iOS 10.0, *))
                    {
                        [self getLineHidden:NO];
                    }else
                    {
                        self.shadowImage = nil;
                    }
                    
                        }
    
}

///5.ios10上关于是否隐藏下划线的处理：
- (void)getLineHidden:(BOOL)hidden

{
    
    [self setShadowImage:[[[UIImage alloc] init] imageWithColor:hidden?[UIColor clearColor]:HEXC(0xD8D8D8)]];
    [self setBackgroundImage:[[[UIImage alloc] init] imageWithColor:hidden?[UIColor clearColor]:HEXC(0xD8D8D8)] forBarMetrics:UIBarMetricsDefault];
    if (@available(iOS 13.0, *)) {
        [self.standardAppearance setShadowImage:[[[UIImage alloc] init] imageWithColor:hidden?[UIColor clearColor]:HEXC(0xD8D8D8)]];
        [self.standardAppearance setBackgroundColor:hidden?[UIColor clearColor]:HEXC(0xD8D8D8)];
        [self.scrollEdgeAppearance setShadowImage:[[[UIImage alloc] init] imageWithColor:hidden?[UIColor clearColor]:HEXC(0xD8D8D8)]];
        [self.scrollEdgeAppearance setBackgroundColor:hidden?[UIColor clearColor]:HEXC(0xD8D8D8)];
    }
    //    UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
    //    [appearance configureWithOpaqueBackground];
    //    appearance.backgroundColor = [UIColor whiteColor];
    //    self.navigationController.navigationBar.standardAppearance = appearance;
    //    self.navigationController.navigationBar.scrollEdgeAppearance=self.navigationController.navigationBar.standardAppearance;
    //    }
    
    [self hiddenSubViewsWithViews:self.subviews Hidden:hidden];
}
-(void)hiddenSubViewsWithViews:(NSArray*)subViews Hidden:(BOOL)hidden{
    for (UIView *view in subViews) {
        if (view.frame.size.height > 0 && view.bounds.size.height<=1){
            view.hidden = hidden;
        }else if (view.subviews.count>0){
            [self hiddenSubViewsWithViews:view.subviews Hidden:hidden];
        }
    }
}
///6.关于设置导航条标题font和color的处理：
- (void)setTitleColor:(UIColor *)titleColor font:(UIFont *)font

{
    self.tintColor = titleColor;
    //    UINavigationBar *bar = [UINavigationBar appearance];
    //        //设置显示的颜色
    //    bar.barTintColor = [UIColor whiteColor];
    //    bar.tintColor = [UIColor whiteColor];
    [self setTitleTextAttributes:@{NSFontAttributeName:font,
                                   NSForegroundColorAttributeName : titleColor
                                   
                                 }];
    [self setShadowImage:[[[UIImage alloc] init] imageWithColor:[UIColor clearColor]]];
    [self setBackgroundImage:[[[UIImage alloc] init] imageWithColor:[UIColor clearColor]] forBarMetrics:UIBarMetricsDefault];
    if (@available(iOS 13.0, *)) {
        self.standardAppearance.titleTextAttributes =@{NSFontAttributeName:font,
                                                       NSForegroundColorAttributeName : titleColor
                                                       
        };
        self.scrollEdgeAppearance.titleTextAttributes =@{NSFontAttributeName:font,
                                                         NSForegroundColorAttributeName : titleColor
                                                         
        };
        //    UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
        //    [appearance configureWithOpaqueBackground];
        //    appearance.backgroundColor = [UIColor whiteColor];
        //    self.navigationController.navigationBar.standardAppearance = appearance;
        //    self.navigationController.navigationBar.scrollEdgeAppearance=self.navigationController.navigationBar.standardAppearance;
        //    }
        //    NSArray *subViews = self.navigationController.navigationBar.subviews;
        //      for (UIView *view in subViews) {
        //        if ([view isKindOfClass:[UIImageView class]] && view.bounds.size.height<1){
        //            view.hidden = YES;
        //        }
    }
}

///7.传入一个返回按钮图片名字，自定义导航条返回按钮样式的处理：

- (void)setBackImageWithImageName:(NSString *)imageName

{
    
        UIImage *image = [UIImage imageNamed:imageName];
    
        image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
        if (@available(iOS 13.0, *))
        
            {
            
                    [self.standardAppearance setBackIndicatorImage:image transitionMaskImage:image];
            
                }else
                
                    {
                    
                            self.backIndicatorImage = image;
                    
                            self.backIndicatorTransitionMaskImage = image;
                    
                        }
    
}
@end
